#include "ViewManager.h"
#include "IView.h"

using namespace Views;

ViewManager::ViewManager(const std::string &resource_dir)
	: resource_dir(resource_dir)
{
}

ViewManager::~ViewManager()
{
	while(!active_view.empty())
		active_view.pop();
	while(!active_gui_view.empty())
		active_gui_view.pop();
}

void ViewManager::update(float dt)
{
	if(!active_view.empty())
		active_view.top()->update(dt);
	if(!active_gui_view.empty())
		active_gui_view.top()->update(dt);
}

void ViewManager::render()
{
	if(!active_view.empty())
		active_view.top()->render();
	if(!active_gui_view.empty())
		active_gui_view.top()->render();
}

void ViewManager::add(IView *view)
{
	views.push_back(std::shared_ptr<IView>(view));
}

void ViewManager::pushActive(const std::string &view_id)
{
	for(unsigned int i = 0; i < views.size(); i++)
	{
		if(views[i]->getId() == view_id)
		{
			active_view.push(views[i].get());
			break;
		}
	}
}

void ViewManager::pushActiveGui(const std::string &view_id)
{
	for(unsigned int i = 0; i < views.size(); i++)
	{
		if(views[i]->getId() == view_id)
		{
			active_gui_view.push(views[i].get());
			break;
		}
	}
}

void ViewManager::popActive()
{
	if(!active_view.empty())
		active_view.pop();
}

void ViewManager::popActiveGui()
{
	if(!active_gui_view.empty())
		active_gui_view.pop();
}

void ViewManager::keyInput(int key, int pressed)
{
	bool used = false;
	if(!active_gui_view.empty())
		used = active_gui_view.top()->keyInput(key,pressed);

	if(!used && !active_view.empty())
		active_view.top()->keyInput(key,pressed);
}

void ViewManager::mouseButtonInput(int button, int pressed)
{
	bool used = false;
	if(!active_gui_view.empty())
		used = active_gui_view.top()->mouseButtonInput(button,pressed);

	if(!used && !active_view.empty())
		active_view.top()->mouseButtonInput(button,pressed);
}

void ViewManager::mouseMoveInput(int x, int y)
{
	bool used = false;
	if(!active_gui_view.empty())
		used = active_gui_view.top()->mouseMoveInput(x, y);

	if(!used && !active_view.empty())
		active_view.top()->mouseMoveInput(x, y);
}

